package com.example.tongue.ui.Online.Body;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.tongue.ui.Online.Body.utils.ScoreCalculationSeven;
import com.example.tongue.R;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Idiosyncratic extends Activity implements View.OnClickListener {
    private Button next7,whetherIdiosyncratic;
    private RadioGroup sneeze,stuffyNose,
            cameoDueToOdor,proneToAllergiesStool,
            urticaria,purpura,
            scratch;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.idiosyncratic);
        //绑定控件资源
        next7=findViewById(R.id.Next7);

                sneeze=findViewById(R.id.Sneeze);
                stuffyNose=findViewById(R.id.StuffyNose);
                cameoDueToOdor=findViewById(R.id.CameoDueToOdor);
                proneToAllergiesStool=findViewById(R.id.ProneToAllergiesStool);
                urticaria=findViewById(R.id.Urticaria);
                purpura=findViewById(R.id.Purpura);
                scratch=findViewById(R.id.Scratch);

        //设置监听器
        next7.setOnClickListener(this);

                sneeze.setOnClickListener(this);
                stuffyNose.setOnClickListener(this);
                cameoDueToOdor.setOnClickListener(this);
                proneToAllergiesStool.setOnClickListener(this);
                urticaria.setOnClickListener(this);
                purpura.setOnClickListener(this);
                scratch.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.Next7:{
                //计算结果,把结果写入缓存
                IdiosyncraticResult();
//                Toast.makeText(Idiosyncratic.this, "进入气淤质", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(Idiosyncratic.this,GasSilt.class));
            }
            break;

        }

    }
    public void IdiosyncraticResult(){
        int    sneezeid=    sneeze.getCheckedRadioButtonId();
        RadioButton   sneezebtn= findViewById(   sneezeid);
        String    sneezefin=   sneezebtn.getText().toString();

        int     stuffyNoseid=    stuffyNose.getCheckedRadioButtonId();
        RadioButton  stuffyNosebtn= findViewById( stuffyNoseid);
        String  stuffyNosefin= stuffyNosebtn.getText().toString();

        int  cameoDueToOdorid=   cameoDueToOdor.getCheckedRadioButtonId();
        RadioButton  cameoDueToOdorbtn= findViewById(cameoDueToOdorid);
        String  cameoDueToOdorfin=   cameoDueToOdorbtn.getText().toString();

        int  proneToAllergiesStoolid= proneToAllergiesStool.getCheckedRadioButtonId();
        RadioButton proneToAllergiesStoolbtn= findViewById( proneToAllergiesStoolid);
        String proneToAllergiesStoolfin= proneToAllergiesStoolbtn.getText().toString();

        int  urticariaid= urticaria.getCheckedRadioButtonId();
        RadioButton urticariabtn= findViewById( urticariaid);
        String urticariafin= urticariabtn.getText().toString();


        int    purpuraid=   purpura.getCheckedRadioButtonId();
        RadioButton     purpurabtn= findViewById(  purpuraid);
        String     purpurafin=    purpurabtn.getText().toString();

        int  scratchid= scratch.getCheckedRadioButtonId();
        RadioButton  scratchbtn= findViewById( scratchid);
        String  scratchfin= scratchbtn.getText().toString();





        Map<String,Integer>  sneezeMap=new HashMap<String,Integer>();
        sneezeMap.put("没有",1);
        sneezeMap.put("很少",2);
        sneezeMap.put("有时",3);
        sneezeMap.put("常常",4);
        sneezeMap.put("经常",5);

        Map<String,Integer>   stuffyNoseMap=new HashMap<String,Integer>();
        stuffyNoseMap.put("没有",1);
        stuffyNoseMap.put("很少",2);
        stuffyNoseMap.put("有时",3);
        stuffyNoseMap.put("常常",4);
        stuffyNoseMap.put("经常",5);

        Map<String,Integer>  cameoDueToOdorMap=new HashMap<String,Integer>();
        cameoDueToOdorMap.put("没有",1);
        cameoDueToOdorMap.put("很少",2);
        cameoDueToOdorMap.put("有时",3);
        cameoDueToOdorMap.put("常常",4);
        cameoDueToOdorMap.put("经常",5);

        Map<String,Integer> proneToAllergiesStoolMap=new HashMap<String,Integer>();
        proneToAllergiesStoolMap.put("没有",1);
        proneToAllergiesStoolMap.put("很少",2);
        proneToAllergiesStoolMap.put("有时",3);
        proneToAllergiesStoolMap.put("常常",4);
        proneToAllergiesStoolMap.put("经常",5);

        Map<String,Integer> urticariaMap=new HashMap<String,Integer>();
        urticariaMap.put("没有",1);
        urticariaMap.put("很少",2);
        urticariaMap.put("有时",3);
        urticariaMap.put("常常",4);
        urticariaMap.put("经常",5);

        Map<String,Integer>  purpuraMap=new HashMap<String,Integer>();
        purpuraMap.put("没有",1);
        purpuraMap.put("很少",2);
        purpuraMap.put("有时",3);
        purpuraMap.put("常常",4);
        purpuraMap.put("经常",5);

        Map<String,Integer> scratchMap=new HashMap<String,Integer>();
        scratchMap.put("没有",1);
        scratchMap.put("很少",2);
        scratchMap.put("有时",3);
        scratchMap.put("常常",4);
        scratchMap.put("经常",5);







        Integer idiosyncraticresult =  sneezeMap.get(  sneezefin)+
                stuffyNoseMap.get(  stuffyNosefin) +
                cameoDueToOdorMap.get(   cameoDueToOdorfin) +
                proneToAllergiesStoolMap.get( proneToAllergiesStoolfin)+
                urticariaMap.get(   urticariafin)+
                purpuraMap.get(  purpurafin)+
                scratchMap.get(   scratchfin)
                ;
        ScoreCalculationSeven scoreCalculationSeven=new ScoreCalculationSeven(idiosyncraticresult);
        String ConversionScore=scoreCalculationSeven.getResult();
        TextView idiosyncraticResult=(TextView)findViewById(R.id.IdiosyncraticResult);
        idiosyncraticResult.setText("特禀质结果:"+ConversionScore);

        File dataFileDir=new File(getExternalCacheDir(),"PhysiqueDataFile");
        FileWriter writer=null;
        try {
            //开启写入流
            writer=new FileWriter(dataFileDir+"/PhysiqueData.txt",true);
            writer.write(ConversionScore+"\n");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                System.out.println("数据文件已生成");
            }
        }
    }
}
